package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.feign.vo.req.FeignSelectEmployeeListRequestVO;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.gts.bpaas.framework.api.rest.dto.ResultResponse;
import com.aliyun.gts.delivery.enterprise.sdk.common.PageQuery;
import com.aliyun.gts.delivery.enterprise.sdk.common.PageResult;
import com.aliyun.gts.delivery.enterprise.sdk.orgaccount.OrgAccountService;
import com.aliyun.gts.delivery.enterprise.sdk.orgaccount.in.AccountInfoQuery;
import com.aliyun.gts.delivery.enterprise.sdk.orgaccount.in.OrganizationInfoQuery;
import com.aliyun.gts.delivery.enterprise.sdk.orgaccount.in.OrganizationTreeQuery;
import com.aliyun.gts.delivery.enterprise.sdk.orgaccount.out.AccountInfo;
import com.aliyun.gts.delivery.enterprise.sdk.orgaccount.out.OrganizationInfo;
import com.aliyun.gts.enterprise.basic.FouraUser;
import com.aliyun.gts.enterprise.basic.OrgAccountBasicAuthService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.WaterOrgAccountService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.foura.WaterOrgAccountServiceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2021/1/5 下午2:02
 */
@Service
public class OrgAccountServiceAdapter implements InitializingBean {

    @Autowired(required = false)
    private OrgAccountService orgAccountService;

    public ResultResponse<AccountInfo> getCurrentAccountInfo() {
        return orgAccountService.getCurrentAccountInfo();
    }

    public ResultResponse<AccountInfo> getAccountInfoByCode(String s) {
        return orgAccountService.getAccountInfoByCode(s);
    }

    public ResultResponse<PageResult<AccountInfo>> queryAccountInfoPage(PageQuery pageQuery, AccountInfoQuery accountInfoQuery) {
        return orgAccountService.queryAccountInfoPage(pageQuery, accountInfoQuery);
    }

    public ResultResponse<OrganizationInfo> getOrganizationByCode(String s) {
        return orgAccountService.getOrganizationByCode(s);
    }

    public ResultResponse<OrganizationInfo> getOrganizationTree(OrganizationTreeQuery organizationTreeQuery) {
        return orgAccountService.getOrganizationTree(organizationTreeQuery);
    }

    public ResultResponse<PageResult<OrganizationInfo>> queryOrganizationInfoPage(PageQuery pageQuery, OrganizationInfoQuery organizationInfoQuery) {
        return orgAccountService.queryOrganizationInfoPage(pageQuery, organizationInfoQuery);
    }

    public ResultResponse<List<OrganizationInfo>> getAccountBelongOrgByAccountCode(String s) {
        return orgAccountService.getAccountBelongOrgByAccountCode(s);
    }

    public ResultResponse<PageResult<AccountInfo>> getOrganizationMembersByOrgCode(PageQuery pageQuery, String s) {
        return orgAccountService.getOrganizationMembersByOrgCode(pageQuery, s);
    }

    public ResultResponse<List<AccountInfo>> getAccountDirectSubordinates(String s) {
        return orgAccountService.getAccountDirectSubordinates(s);
    }

    public ResultResponse<List<AccountInfo>> getAccountListByCode(String s) {
        return orgAccountService.getAccountListByCode(s);
    }

    public ResultResponse<OrganizationInfo> getRootOrganization() {
        return orgAccountService.getRootOrganization();
    }

    public ResultResponse<OrganizationInfo> getAllOrgInfo() {
        return orgAccountService.getAllOrgInfo();
    }

    public ResultResponse<List<OrganizationInfo>> getOrganizationInfoByParentCode(String s) {
        return orgAccountService.getOrganizationInfoByParentCode(s);
    }

    public ResultResponse<List<AccountInfo>> getAllUserInfo(Integer integer) {
        return orgAccountService.getAllUserInfo(integer);
    }
    public ResultResponse<List<AccountInfo>> getAccountByNameFuzzy(String username){
        if (orgAccountService instanceof WaterOrgAccountService) {
            return ((WaterOrgAccountServiceImpl)orgAccountService).getAccountByNameFuzzy(username);
        }
        String s = "{\"success\":true,\"errCode\":0,\"errMsg\":\"success\",\"totalCount\":0,\"datas\":[{\"code\":\"17801130126\",\"username\":\"超级管理员\",\"displayName\":\"超级管理员\",\"email\":\"\",\"mobilePhone\":\"1234567890\",\"extentAttributes\":{\"CARD_NO\":\"\",\"ORG_ID\":\"1919f623b5d140598b4eb80660715055\",\"USER_STATUS\":\"1\",\"USER_SEX\":\"2\",\"OFFICE_EMAIL\":\"\",\"USER_MOBILE_LONG\":\"1234567890\",\"PHONE\":\"1234567890\",\"USER_ID\":\"17801130126\",\"EXTRA_ATTRIBUTES\":\"\",\"USER_MAIN_ID\":\"\",\"USER_PART_TYPE\":\"\",\"FULL_NAME\":\"BJZG\",\"ORDER_INDEX\":\"\",\"USER_JOB_DESC\":\"\",\"GACN\":\"\",\"ORG_NAME\":\"BJZG\",\"USER_BASE_STATUS\":\"\",\"USER_NAME\":\"超级管理员\",\"USER_CODE\":\"17801130126\",\"USER_TYPE\":\"\",\"USER_POLICE_ID\":\"\",\"ORG_CODE\":\"310000\",\"USER_INDATE\":\"20190531000000+0800\",\"OFFICE_ADDRESS\":\"\",\"USER_DATA_LEVEL\":\"\",\"USER_QUATER\":\"\"}},{\"code\":\"15521152565\",\"username\":\"霍志强\",\"displayName\":\"霍志强\",\"email\":\"\",\"mobilePhone\":\"17801130126\",\"extentAttributes\":{\"CARD_NO\":\"\",\"ORG_ID\":\"1919f623b5d140598b4eb80660715055\",\"USER_STATUS\":\"1\",\"USER_SEX\":\"1\",\"OFFICE_EMAIL\":\"\",\"USER_MOBILE_LONG\":\"17801130126\",\"PHONE\":\"17801130126\",\"USER_ID\":\"15521152565\",\"EXTRA_ATTRIBUTES\":\"\",\"USER_MAIN_ID\":\"\",\"USER_PART_TYPE\":\"\",\"FULL_NAME\":\"BJZG\",\"ORDER_INDEX\":\"\",\"USER_JOB_DESC\":\"\",\"GACN\":\"\",\"ORG_NAME\":\"BJZG\",\"USER_BASE_STATUS\":\"\",\"USER_NAME\":\"霍志强\",\"USER_CODE\":\"15521152565\",\"USER_TYPE\":\"\",\"USER_POLICE_ID\":\"\",\"ORG_CODE\":\"310000\",\"USER_INDATE\":\"20190903000000+0800\",\"OFFICE_ADDRESS\":\"\",\"USER_DATA_LEVEL\":\"\",\"USER_QUATER\":\"\"}},{\"code\":\"18279440960\",\"username\":\"张明珠\",\"displayName\":\"张明珠\",\"email\":\"\",\"mobilePhone\":\"18279440960\",\"extentAttributes\":{\"CARD_NO\":\"\",\"ORG_ID\":\"1919f623b5d140598b4eb80660715055\",\"USER_STATUS\":\"1\",\"USER_SEX\":\"2\",\"OFFICE_EMAIL\":\"\",\"USER_MOBILE_LONG\":\"18279440960\",\"PHONE\":\"18279440960\",\"USER_ID\":\"18279440960\",\"EXTRA_ATTRIBUTES\":\"\",\"USER_MAIN_ID\":\"\",\"USER_PART_TYPE\":\"\",\"FULL_NAME\":\"BJZG\",\"ORDER_INDEX\":\"\",\"USER_JOB_DESC\":\"\",\"GACN\":\"\",\"ORG_NAME\":\"BJZG\",\"USER_BASE_STATUS\":\"\",\"USER_NAME\":\"张明珠\",\"USER_CODE\":\"18279440960\",\"USER_TYPE\":\"\",\"USER_POLICE_ID\":\"\",\"ORG_CODE\":\"310000\",\"USER_INDATE\":\"\",\"OFFICE_ADDRESS\":\"\",\"USER_DATA_LEVEL\":\"\",\"USER_QUATER\":\"\"}},{\"code\":\"13111111189\",\"username\":\"李四appcode\",\"displayName\":\"李四appcode\",\"email\":\"111@qq.com\",\"mobilePhone\":\"13111111189\",\"extentAttributes\":{\"CARD_NO\":\"\",\"ORG_ID\":\"1919f623b5d140598b4eb80660715055\",\"USER_STATUS\":\"1\",\"USER_SEX\":\"1\",\"OFFICE_EMAIL\":\"111@qq.com\",\"USER_MOBILE_LONG\":\"13111111189\",\"PHONE\":\"13111111189\",\"USER_ID\":\"13111111189\",\"EXTRA_ATTRIBUTES\":\"\",\"USER_MAIN_ID\":\"\",\"USER_PART_TYPE\":\"\",\"FULL_NAME\":\"BJZG\",\"ORDER_INDEX\":\"\",\"USER_JOB_DESC\":\"\",\"GACN\":\"\",\"ORG_NAME\":\"BJZG\",\"USER_BASE_STATUS\":\"\",\"USER_NAME\":\"李四appcode\",\"USER_CODE\":\"13111111189\",\"USER_TYPE\":\"\",\"USER_POLICE_ID\":\"\",\"ORG_CODE\":\"310000\",\"USER_INDATE\":\"\",\"OFFICE_ADDRESS\":\"\",\"USER_DATA_LEVEL\":\"\",\"USER_QUATER\":\"\"}},{\"code\":\"13211511110\",\"username\":\"张三appcode\",\"displayName\":\"张三appcode\",\"email\":\"111@qq.com\",\"mobilePhone\":\"13211511110\",\"extentAttributes\":{\"CARD_NO\":\"\",\"ORG_ID\":\"1919f623b5d140598b4eb80660715055\",\"USER_STATUS\":\"1\",\"USER_SEX\":\"1\",\"OFFICE_EMAIL\":\"111@qq.com\",\"USER_MOBILE_LONG\":\"13211511110\",\"PHONE\":\"13211511110\",\"USER_ID\":\"13211511110\",\"EXTRA_ATTRIBUTES\":\"\",\"USER_MAIN_ID\":\"\",\"USER_PART_TYPE\":\"\",\"FULL_NAME\":\"BJZG\",\"ORDER_INDEX\":\"\",\"USER_JOB_DESC\":\"\",\"GACN\":\"\",\"ORG_NAME\":\"BJZG\",\"USER_BASE_STATUS\":\"\",\"USER_NAME\":\"张三appcode\",\"USER_CODE\":\"13211511110\",\"USER_TYPE\":\"\",\"USER_POLICE_ID\":\"\",\"ORG_CODE\":\"310000\",\"USER_INDATE\":\"\",\"OFFICE_ADDRESS\":\"\",\"USER_DATA_LEVEL\":\"\",\"USER_QUATER\":\"\"}}],\"pageNum\":1,\"pageSize\":10}";
        return JSONObject.parseObject(s,new TypeReference<ResultResponse<List<AccountInfo>>>(){});
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        if (Objects.isNull(orgAccountService)) {
            //
            this.orgAccountService = new DefaultOrgAccountService();
        }
    }

    public ResultResponse getAllUserGroupList(){
        if (orgAccountService instanceof WaterOrgAccountServiceImpl) {
            return ((WaterOrgAccountServiceImpl)orgAccountService).getAllUserGroupList();
        }
        return ResultResponse.succResult();
    }

    private static class DefaultOrgAccountService extends OrgAccountBasicAuthService {

        @Override
        public FouraUser getUserInfo() {
            return new FouraUser("admin-simple",
                    "admin-simple", "admin-simple",
                    "admin-simple", "13291111111");
        }
    }

    public ResultResponse<List<OrganizationInfo>> getRootOrg() {
        if (orgAccountService instanceof  WaterOrgAccountServiceImpl){
            return ((WaterOrgAccountServiceImpl)orgAccountService).getRootOrg();
        }
        ResultResponse<OrganizationInfo> rootOrganization = this.getRootOrganization();
        return ResultResponse.succResult(Arrays.asList(rootOrganization.getData()));
    }

}
